<template>
  <div>
    <a-modal v-model="show" title="同步" @ok="onUploadData">
      <a-form-model layout="inline">
        <a-form-model-item label="地址">
          <a-input v-model="svrAddr" />
        </a-form-model-item>
        <a-form-model-item label="用户">
          <a-input v-model="username" />
        </a-form-model-item>
        <a-form-model-item label="密码">
          <a-input v-model="password" />
        </a-form-model-item>
        <a-form-model-item label="站点">
          <a-tree-select
            style="width: 200px"
            v-model="stationId"
            :tree-data="remoteStations"
            placeholder="Please select"
            tree-default-expand-all
          />
          <a-button @click="onLoadData">从上级获取站点</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>
<script>
import { syncToCloud, getCloudStations } from "@/services/proxy";
import { getEx } from "@/services/crud";
export default {
  data() {
    return {
      show: false,
      svrAddr: "127.0.0.1:18866",
      username: "IOT2021",
      password: "Mima2021@)@!",
     
      parent: null,
      param: null,
      stationId: undefined,
      remoteStations: [],
      edgeID: "",
    };
  },
  methods: {
  
    onUploadData() {
      let self = this;
      getEx("/thingQuery/cfg").then((res) => {
        if (res?.data?.result?.things) {
          let edge = res.data.result.things.find((r) => {
            return r?.thing?.model_id === "IOTBASEmEdgeCTL";
          });
          if (edge?.thing) {
            let option = {
              local_cfg: res.data.result,
              station_id: self.stationId,
             
            };
            syncToCloud({
              pd: this.password,
              un: this.username,
              addr: this.svrAddr,
              things: option,
              edgeID: edge.thing.uid,
            }).then(() => {
              self.show = false;
            });
          }
        }
      });
    },
    onLoadData() {
      let self = this;
      getCloudStations({
        pd: this.password,
        un: this.username,
        addr: this.svrAddr,
      }).then((res) => {
        console.log("-------------------", res);
        if (res && res.data && res.data.result) {
          //self.remoteStations = res.data.result
          let tree = [];
          let maptree = {};
          res.data.result.forEach((element) => {
            element.title = element.s_name;
            element.key = element.uid;
            element.value = element.key;
            maptree[element.key] = element;
            if (element.parent_id == "" || !element.parent_id) {
              tree.push(element);
            }
          });
          res.data.result.forEach((element) => {
            if (element.parent_id && element.parent_id != "") {
              let parent = maptree[element.parent_id];
              if (parent) {
                if (!parent.children) {
                  parent.children = [];
                }
                parent.children.push(element);
              }
            }
          });
          self.remoteStations = tree;
        }
      });
    },
    showDlg() {
     
      //this.parent = parent;
      this.show = true;
      //this.param = param;
    },
  },
};
</script>